//
//  InterViewSkillVC.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "InterViewSkillVC.h"
#import "InterViewSkillAPIManger.h"
#import "HeaderView.h"
#import "customCell.h"
#import "InterViewSkilDetailVC.h"

static NSString *const kHeaderFooterViewWithIdentifier = @"HeaderFooterViewWithIdentifier";
static NSString *const kReusableCellWithIdentifier = @"cellid";
static NSString *const kInterViewSkilDetailVC = @"InterViewSkilDetailVC";
@interface InterViewSkillVC ()<UITableViewDataSource,UITableViewDelegate,HeaderViewDelegate>
{
    NSArray *_types;
}
@property (nonatomic, strong) NSString *techniquesId;
@property (nonatomic, strong) NSString *techniquesTitle;
@property (nonatomic, copy) NSDictionary *dataList;
@property (nonatomic, copy) NSArray *sectionTitles;
@property (nonatomic, copy) NSMutableDictionary  *openDict; /**< 用于设置当前分组展开或关闭 */
@property (nonatomic, strong) InterViewSkillAPIManger *interViewSkillManger;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

- (void)initializeDataSource;     /**< 初始化数据源 */
- (void)initializeUserIntefcace;  /**< 初始化用户界面 */

@end

@implementation InterViewSkillVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserIntefcace];
}
- (void)initializeDataSource{
    
    [self.interViewSkillManger startInterViewSkillRequestWithPaprameters:@{} responds:^(RespondsStatus status, NSString *message, id reslutData) {
        if (status == RespondsStatusSuccess) {
            self.dataList = reslutData;
            
            _types = [_dataList allKeys];
            
            [self.tableView reloadData];
        }
        NSLog(@"%@",message);
    }];
}
- (void)initializeUserIntefcace{
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReusableCellWithIdentifier];
    [self.tableView registerClass:[HeaderView class] forHeaderFooterViewReuseIdentifier:kHeaderFooterViewWithIdentifier];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _types.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    NSString *name = _types[section];
    
    return [_dataList[name] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReusableCellWithIdentifier];
    NSString *name = _types[indexPath.section];
    cell.layer.masksToBounds = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.textLabel.text = _dataList[name][indexPath.row][@"techniquesTitle"];
    
    return cell;
    
}

#pragma mark - UITableViewDelegate

// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 根据当前组获取字典key
    NSString *key = [NSString stringWithFormat:@"%ld", indexPath.section];
    
    // 判断如果字典对应key是否有值，如果有值，说明当前组展开，返回 80 高度；否则为关闭，返回 0 高度；
    if ([_openDict objectForKey:key]) {
        return 80;
    }else {
        return 0;
    }
}

// 设置组高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
// 自定义头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHeaderFooterViewWithIdentifier];
    headerView.delegate = self;
    headerView.sectionLabel.text = _types[section];
    headerView.gestureImageView.image = [UIImage imageNamed:@"inter_close"];
    headerView.tag = section;
    NSString *key = [NSString stringWithFormat:@"%ld",section];
    [UIView animateWithDuration:0.4 animations:^{
        headerView.gestureImageView.image = [_openDict objectForKey:key] ? [UIImage imageNamed:@"inter_close"] :[UIImage imageNamed:@"inter_open"] ;
    }];
    return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *name = _types[indexPath.section];
   _techniquesId = _dataList[name][indexPath.row][@"techniquesId"];
    _techniquesTitle = _dataList[name][indexPath.row][@"techniquesTitle"];
    [self performSegueWithIdentifier:kInterViewSkilDetailVC sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:[InterViewSkilDetailVC class]]) {
        InterViewSkilDetailVC *ivsVc = segue.destinationViewController;
        ivsVc.techniquesId = _techniquesId;
        ivsVc.techniquesTitle = _techniquesTitle;
    }
}
#pragma mark *** HeaderViewDelegate ***
- (void)headerView:(HeaderView *)headerView didSelecedWithTag:(NSInteger)tag{
    // 判断字典是否存在，如果不存在，则新建；
    // 如果对一个未初始化的可变字典作数据操作，将会导致奔溃；
    if (!_openDict) {
        _openDict = [NSMutableDictionary dictionary];
    }
    
    // 获取tag值，并将其作为字典key
    NSString *key = [NSString stringWithFormat:@"%ld", tag];
    
    // 模拟展开与关闭
    // 判断当前组是否有值，有值则移除对应key-value对，无值则设置key-value对。
    if (![_openDict objectForKey:key]) {
        [_openDict setObject:key forKey:key];
    }else{
        [_openDict removeObjectForKey:key];
    }
    
    // 刷新表格视图指定组
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:tag] withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark - getters
- (InterViewSkillAPIManger *)interViewSkillManger{
    if (!_interViewSkillManger) {
        _interViewSkillManger = [[InterViewSkillAPIManger alloc] init];
    }
    return _interViewSkillManger;
}

@end
